package com.meilai.project.service.business.customer.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.meilai.project.dto.business.customer.CustomerFollowDTO;
import com.meilai.project.dto.business.customer.CustomerFollowTmpDTO;
import com.meilai.project.entity.business.customer.CustomerFollow;
import com.meilai.project.entity.business.customer.CustomerFollowAdvance;
import com.meilai.project.entity.business.customer.CustomerFollowPhoto;
import com.meilai.project.entity.business.customer.CustomerRecommendedResources;
import com.meilai.project.mapper.business.customer.CustomerFollowMapper;
import com.meilai.project.service.business.customer.CustomerFollowAdvanceService;
import com.meilai.project.service.business.customer.CustomerFollowPhotoService;
import com.meilai.project.service.business.customer.CustomerFollowService;
import com.meilai.project.service.business.customer.CustomerRecommendedResourcesService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 客户跟进信息表 服务实现类
 * </p>
 *
 * @author xhy
 * @since 2022-01-06
 */
@Service
public class CustomerFollowImpl extends ServiceImpl<CustomerFollowMapper, CustomerFollow> implements CustomerFollowService {

    @Autowired
    private CustomerFollowAdvanceService customerFollowAdvanceService;

    @Autowired
    private CustomerRecommendedResourcesService customerRecommendedResourcesService;

    @Autowired
    private CustomerFollowPhotoService customerFollowPhotoService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean saveBatch(List<CustomerFollowTmpDTO> follows, Long id) {
        Boolean flag = true;
        this.remove(Wrappers.<CustomerFollow>lambdaQuery().eq(CustomerFollow::getCustomer_id, id).isNull(CustomerFollow::getDeleted_at));
        for (CustomerFollowTmpDTO dto : follows) {
            CustomerFollow customerFollow = new CustomerFollow();
            BeanUtils.copyProperties(dto, customerFollow);
            customerFollow.setCustomer_id(id);
            flag = flag && this.save(customerFollow);
            customerFollowAdvanceService.saveBatch(dto.getAction_list().stream().map(item -> {
                CustomerFollowAdvance advance = new CustomerFollowAdvance();
                advance.setAdvance_id(item);
                advance.setCustomer_follow_id(customerFollow.getId());
                return advance;
            }).collect(Collectors.toList()));
            customerRecommendedResourcesService.saveBatch(dto.getResource_list().stream().map(item -> {
                CustomerRecommendedResources resources = new CustomerRecommendedResources();
                resources.setResources_id(item);
                resources.setCustomer_follow_id(customerFollow.getId());
                return resources;
            }).collect(Collectors.toList()));
            if (CollectionUtils.isNotEmpty(dto.getUrl_list())) {
                customerFollowPhotoService.saveBatch(dto.getUrl_list().stream().map(url -> {
                    CustomerFollowPhoto photo = new CustomerFollowPhoto();
                    photo.setFollow_id(customerFollow.getId());
                    photo.setUrl(url);
                    return photo;
                }).collect(Collectors.toList()));
            }
        }
        return flag;
    }

    @Override
    public Page<CustomerFollowDTO> page(Integer current_page, Integer page_size, Long customer_id) {
        Page<CustomerFollowDTO> page = new Page<>(current_page, page_size);
        List<CustomerFollowDTO> list = baseMapper.page(page, customer_id);
        if (CollectionUtils.isNotEmpty(list)) {
            List<Long> ids = list.stream().map(CustomerFollowDTO::getId).collect(Collectors.toList());
            Map<Long, List<CustomerFollowPhoto>> map = customerFollowPhotoService.list(Wrappers.<CustomerFollowPhoto>lambdaQuery().in(CustomerFollowPhoto::getFollow_id, ids)).stream().collect(Collectors.groupingBy(CustomerFollowPhoto::getFollow_id));
            list.forEach(item -> {
                if (map.containsKey(item.getId())) {
                    item.setUrl_list(map.get(item.getId()).stream().map(CustomerFollowPhoto::getUrl).collect(Collectors.toList()));
                } else {
                    item.setUrl_list(new ArrayList<>());
                }
            });
        }
        page.setRecords(list);
        return page;
    }

    @Override
    public List<String> getHistoryUser(Long customer_id) {
        return baseMapper.getHistoryUser(customer_id);
    }

    @Override
    public List<CustomerFollow> queryLatestRecord(List<Long> customer_ids) {
        return baseMapper.queryLatestRecord(customer_ids);
    }
}
